home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP06.ZIP / CHAP06 / COSCHMOO / DOCUMENT.CPP < prev    next >
C/C++ Source or Header  |  1993-04-15  |  18KB  |  815 lines

  1. /*
  2.  * DOCUMENT.CPP
  3.  * Component Schmoo Chapter 6
  4.  *
  5.  * Implementation of the CSchmooDoc derivation of CDocument as
  6.  * well as an implementation of CPolylineAdviseSink.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "coschmoo.h"
  19.  
  20.  
  21.  
  22. /*
  23.  * CSchmooDoc::CSchmooDoc
  24.  * CSchmooDoc::~CSchmooDoc
  25.  *
  26.  * Constructor Parameters:
  27.  *  hInst           HINSTANCE of the application.
  28.  */
  29.  
  30. CSchmooDoc::CSchmooDoc(HINSTANCE hInst)
  31.     : CDocument(hInst)
  32.     {
  33.     m_uPrevSize=SIZE_RESTORED;
  34.     m_pPL=NULL;
  35.     m_pPLAdv=NULL;
  36.     m_pIPersistStorage=NULL;
  37.  
  38.     //CHAPTER6MOD
  39.     m_pIAdviseSink=NULL;
  40.     m_dwConn=0;
  41.     //End CHAPTER6MOD
  42.     return;
  43.     }
  44.  
  45.  
  46. CSchmooDoc::~CSchmooDoc(void)
  47.     {
  48.     //CHAPTER6MOD
  49.     LPDATAOBJECT        pIDataObject;
  50.     HRESULT             hr;
  51.  
  52.     //Turn off the advise.
  53.     if (NULL!=m_pPL && 0!=m_dwConn)
  54.         {
  55.         hr=m_pPL->QueryInterface(IID_IDataObject, (LPVOID FAR *)&pIDataObject);
  56.  
  57.         if (SUCCEEDED(hr))
  58.             pIDataObject->DUnadvise(m_dwConn);
  59.         }
  60.  
  61.     if (NULL!=m_pIAdviseSink)
  62.         delete m_pIAdviseSink;
  63.     //End CHAPTER6MOD
  64.  
  65.     if (NULL!=m_pIPersistStorage)
  66.         m_pIPersistStorage->Release();
  67.  
  68.     if (NULL!=m_pPLAdv)
  69.         delete m_pPLAdv;
  70.  
  71.     if (NULL!=m_pPL)
  72.         m_pPL->Release();
  73.  
  74.     CoFreeUnusedLibraries();
  75.     return;
  76.     }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. /*
  84.  * CSchmooDoc::FInit
  85.  *
  86.  * Purpose:
  87.  *  Initializes an already created document window.  The client actually
  88.  *  creates the window for us, then passes that here for further
  89.  *  initialization.
  90.  *
  91.  * Parameters:
  92.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  93.  *
  94.  * Return Value:
  95.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  96.  */
  97.  
  98. BOOL CSchmooDoc::FInit(LPDOCUMENTINIT pDI)
  99.     {
  100.     RECT            rc;
  101.     HRESULT         hr;
  102.     //CHAPTER6MOD
  103.     FORMATETC       fe;
  104.     LPDATAOBJECT    pIDataObject;
  105.     //End CHAPTER6MOD
  106.  
  107.     //Change the stringtable range to our customization.
  108.     pDI->idsMin=IDS_DOCUMENTMIN;
  109.     pDI->idsMax=IDS_DOCUMENTMAX;
  110.  
  111.     //Do default initialization
  112.     if (!CDocument::FInit(pDI))
  113.         return FALSE;
  114.  
  115.     //Create the Polyline Object via COMPOBJ.DLL functions.
  116.     //CHAPTER6MOD
  117.     hr=CoCreateInstance(CLSID_Polyline6, NULL, CLSCTX_INPROC_SERVER
  118.         , IID_IPolyline6, (LPVOID FAR *)&m_pPL);
  119.     //End CHAPTER6MOD
  120.  
  121.     if (FAILED(hr))
  122.         return FALSE;
  123.  
  124.     //Initialize the contained Polyline which creates a window.
  125.     GetClientRect(m_hWnd, &rc);
  126.     InflateRect(&rc, -8, -8);
  127.  
  128.     if (FAILED(m_pPL->Init(m_hWnd, &rc, WS_CHILD | WS_VISIBLE, ID_POLYLINE)))
  129.         return FALSE;
  130.  
  131.  
  132.     //Set up an advise on the Polyline.
  133.     m_pPLAdv=new CPolylineAdviseSink((LPVOID)this, (LPUNKNOWN)this);
  134.  
  135.     if (NULL==m_pPLAdv)
  136.         return FALSE;
  137.  
  138.     m_pPL->SetAdvise(m_pPLAdv);
  139.  
  140.     //Get the IPersistStorage interface on the object for loads & saves.
  141.     hr=m_pPL->QueryInterface(IID_IPersistStorage, (LPVOID FAR *)&m_pIPersistStorage);
  142.  
  143.     if (FAILED(hr))
  144.         return FALSE;
  145.  
  146.     //CHAPTER6MOD
  147.     /*
  148.      * Create an IAdviseSink and send it to the Polyline's IDataObject
  149.      * with the clipboard format for the Polyline (as in IPOLY6.H).
  150.      */
  151.  
  152.     //This is a private macro.
  153.     SETDefFormatEtc(fe, m_cf, TYMED_HGLOBAL);
  154.  
  155.     m_pIAdviseSink=new CImpIAdviseSink((LPVOID)this, (LPUNKNOWN)this);
  156.  
  157.     if (NULL==m_pIAdviseSink)
  158.         return FALSE;
  159.  
  160.     //Set up an advise for the Polyline format
  161.     hr=m_pPL->QueryInterface(IID_IDataObject, (LPVOID FAR *)&pIDataObject);
  162.  
  163.     if (FAILED(hr))
  164.         return FALSE;
  165.  
  166.     pIDataObject->DAdvise(&fe, ADVF_NODATA, m_pIAdviseSink, &m_dwConn);
  167.     pIDataObject->Release();
  168.  
  169.     //End CHAPTER6MOD
  170.  
  171.     return TRUE;
  172.     }
  173.  
  174.  
  175.  
  176.  
  177. //CHAPTER6MOD
  178. //IUnknown interface for all the interfaces we implement in the document
  179.  
  180. /*
  181.  * CSchmooDoc::QueryInterface
  182.  * CSchmooDoc::AddRef
  183.  * CSchmooDoc::Release
  184.  *
  185.  * Purpose:
  186.  *  IUnknown members for the CSchmooDoc implementation.
  187.  */
  188.  
  189. STDMETHODIMP CSchmooDoc::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  190.     {
  191.     *ppv=NULL;
  192.  
  193.     //The document is the unknown
  194.     if (IsEqualIID(riid, IID_IUnknown))
  195.         *ppv=(LPVOID)this;
  196.  
  197.     //Return contained interfaces for others.
  198.     if (IsEqualIID(riid, IID_IPolylineAdviseSink6))
  199.         *ppv=(LPVOID)m_pPLAdv;
  200.  
  201.     if (IsEqualIID(riid, IID_IAdviseSink))
  202.         *ppv=(LPVOID)m_pIAdviseSink;
  203.  
  204.     /*
  205.      * If we actually assign an interface to ppv we need to AddRef it
  206.      * since we're returning a new pointer.
  207.      */
  208.     if (NULL!=*ppv)
  209.         {
  210.         ((LPUNKNOWN)*ppv)->AddRef();
  211.         return NOERROR;
  212.         }
  213.  
  214.     return ResultFromScode(S_FALSE);
  215.     }
  216.  
  217.  
  218. STDMETHODIMP_(ULONG) CSchmooDoc::AddRef(void)
  219.     {
  220.     return ++m_cRef;
  221.     }
  222.  
  223.  
  224. STDMETHODIMP_(ULONG) CSchmooDoc::Release(void)
  225.     {
  226.     /*
  227.      * Since CoSchmoo doesn't use documents like Component Objects, this
  228.      * doesn't do anything except provide a debugging point.
  229.      */
  230.     return --m_cRef;
  231.     }
  232.  
  233. //End CHAPTER6MOD
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240. /*
  241.  * CSchmooDoc::FMessageHook
  242.  *
  243.  * Purpose:
  244.  *  Processes WM_SIZE for the document so we can resize the Polyline.
  245.  *
  246.  * Parameters:
  247.  *  <WndProc Parameters>
  248.  *  pLRes           LRESULT FAR * in which to store the return value
  249.  *                  for the message.
  250.  *
  251.  * Return Value:
  252.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  253.  */
  254.  
  255. BOOL CSchmooDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  256.     , LPARAM lParam, LRESULT FAR *pLRes)
  257.     {
  258.     UINT        dx, dy;
  259.     RECT        rc;
  260.  
  261.     if (WM_SIZE==iMsg)
  262.         {
  263.         //Don't effect the Polyline size to or from minimized state.
  264.         if (SIZE_MINIMIZED!=wParam && SIZE_MINIMIZED !=m_uPrevSize)
  265.             {
  266.             //When we change size, resize any Polyline we hold.
  267.             dx=LOWORD(lParam);
  268.             dy=HIWORD(lParam);
  269.  
  270.             /*
  271.              * If we are getting WM_SIZE in response to a Polyline
  272.              * notification, then don't resize the Polyline window again.
  273.              */
  274.             if (!m_fNoSize && NULL!=m_pPL)
  275.                 {
  276.                 //Resize the polyline to fit the new client
  277.                 SetRect(&rc, 8, 8, dx-8, dy-8);
  278.                 m_pPL->RectSet(&rc, FALSE);
  279.  
  280.                 /*
  281.                  * We consider sizing something that makes the file dirty,
  282.                  * but not until we've finished the create process, which
  283.                  * is why we set fNoDirty to FALSE in WM_CREATE since we
  284.                  * get a WM_SIZE on the first creation.
  285.                  */
  286.                 if (!m_fNoDirty)
  287.                     FDirtySet(TRUE);
  288.  
  289.                 SetRect(&rc, 0, 0, dx, dy);
  290.  
  291.                 if (NULL!=m_pAdv)
  292.                     m_pAdv->OnSizeChange((LPCDocument)this, &rc);
  293.  
  294.                 m_fNoDirty=FALSE;
  295.                 }
  296.             }
  297.  
  298.         m_uPrevSize=wParam;
  299.         }
  300.  
  301.     /*
  302.      * We return FALSE even on WM_SIZE so we can let the default procedure
  303.      * handle maximized MDI child windows appropriately.
  304.      */
  305.     return FALSE;
  306.     }
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. /*
  316.  * CSchmooDoc::Clear
  317.  *
  318.  * Purpose:
  319.  *  Sets all contents in the document back to defaults with no filename.
  320.  *
  321.  * Paramters:
  322.  *  None
  323.  *
  324.  * Return Value:
  325.  *  None
  326.  */
  327.  
  328. void CSchmooDoc::Clear(void)
  329.     {
  330.     //Completely reset the polyline
  331.     m_pPL->New();
  332.  
  333.     CDocument::Clear();
  334.     return;
  335.     }
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342. /*
  343.  * CSchmooDoc::ULoad
  344.  *
  345.  * Purpose:
  346.  *  Loads a given document without any user interface overwriting the
  347.  *  previous contents of the Polyline window.  We do this by opening
  348.  *  the file and telling the Polyline to load itself from that file.
  349.  *
  350.  * Parameters:
  351.  *  fChangeFile     BOOL indicating if we're to update the window title
  352.  *